

package persistencia;

/**
 * @author aldiene
 */


import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import modelo.Exercicio;
import modelo.Usuario;

public class ExercicioDAO {

    //1= SALVA DADOS NO BANCO
    public static boolean salvar(Exercicio exercicio) {
        
        boolean result = false;
        try {
            Connection con = Conexao.getConexao();
            PreparedStatement stmt =
                    con.prepareStatement("insert into \n" +
                    "EXERCICIO (ANO, MES, ID_USUARIO) values (?, ?, ?)");
            stmt.setInt(1, exercicio.getAno());
            stmt.setInt(2, exercicio.getMes());
            Usuario u = exercicio.getUsuario();
            stmt.setInt(3, u.getId_usuario());
            
            stmt.executeUpdate();
            result = true;
            System.out.println("Exercicio cadastrado com sucesso");
            
        } catch (SQLException ex) {
            System.err.println(ex);
            result = false;
        } finally {
            System.out.println("Fechando conexao");
            Conexao.desconecta();
        }
        return result;
    }
    
    //2= LISTAGEM DO EXERCICIO
    public static Vector getExercicio() {
        
        Vector vp = new Vector();

        try {
            Connection con = Conexao.getConexao();
            Statement stmt =
                    con.createStatement();
            String query = ("select * from EXERCICIO ORDER BY ID_EXERCICIO ASC");
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
                Exercicio exercicio = new Exercicio();
                
                exercicio.setId_exercicio(Integer.parseInt(rs.getString("ID_EXERCICIO")));
                exercicio.setAno(rs.getInt("ANO"));
                exercicio.setMes(rs.getInt("MES"));
                
               
                vp.add(exercicio);
            }
            
        } catch (SQLException ex) {
            System.out.println("Problemas com o BD:");
            ex.printStackTrace();
        }finally{
            Conexao.desconecta();
        }
        return vp;
  }

   //3= EXCLUIR       
    public static void excluir(int id_exercicio) {
        try {
            Connection con = Conexao.getConexao();
            PreparedStatement stmt =
                    con.prepareStatement("delete from EXERCICIO where ID_EXERCICIO = " + id_exercicio);
                       stmt.executeUpdate();
        } catch (SQLException ex) {
            System.err.println(ex);
       } finally {
            Conexao.desconecta();

        }
    }
     //4= RECUPERA 1 EXERCICIO
     public static Exercicio getExercicio(int id_exercicio) {
            Exercicio exercicio = new Exercicio();
        System.out.println("Listar exerccio");
        try {
            Connection con = Conexao.getConexao();
            PreparedStatement stmt =
                    con.prepareStatement("select * from EXERCICIO where ID_EXERCICIO=" + id_exercicio);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
               
                exercicio.setId_exercicio(Integer.parseInt(rs.getString("ID_EXERCICIO")));
                exercicio.setAno(rs.getInt("ANO"));
               exercicio.setMes(rs.getInt("MES"));
            }
            con.close();
          } catch (SQLException ex) {
            System.out.println("Problemas com o BD:");
            ex.printStackTrace();
        }
         return exercicio;
    }   
    
    public static void main(String[] args){
        UsuarioDAO dao = new UsuarioDAO();
        Usuario usuario = dao.getRecuperar(16);
        int ano = 2019;   
        int mes = 11;
        ExercicioDAO e = new ExercicioDAO();
        
        Exercicio exercicio = new Exercicio(usuario, ano, mes);
        exercicio.setId_exercicio(2);
       System.out.println(exercicio);
        e.salvar(exercicio);
         
         Vector v =e.getExercicio();
         Iterator it = v.iterator();
         
        while (it.hasNext()) {
           Exercicio d = (Exercicio) it.next();
           System.out.println(d);
        } 
            v = e.getExercicio();
        it = v.iterator();
        while (it.hasNext()) {
            Exercicio d = (Exercicio) it.next();
           
    }
       
      
            
    }

}
